package com.test.prolenders.bean_prolenders.process_prolenders;

import static com.test.prolenders.config_prolenders.UserState_prolenders.State_Fail;
import static com.test.prolenders.config_prolenders.UserState_prolenders.State_NoOrder;
import static com.test.prolenders.config_prolenders.UserState_prolenders.State_Rp;
import static com.test.prolenders.config_prolenders.UserState_prolenders.State_Wait;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.status_prolenders.ApplyStatus_prolenders;
import com.test.prolenders.status_prolenders.OrderStatus_prolenders;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ProcessInfo_prolenders {

    private static class Inner {
        private static ProcessInfo_prolenders instance = new ProcessInfo_prolenders();
    }

    private ProcessInfo_prolenders() {}

    public static ProcessInfo_prolenders getInstance() {
        return Inner.instance;
    }

    public static void parse(JSONObject jsonObject) {
        String jsonStr = jsonObject.toString();
        Gson gson = new Gson();
        Inner.instance = gson.fromJson(jsonStr, ProcessInfo_prolenders.class);
        try {
            Inner.instance.srcJSON = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //判断当前订单状态显示对应的界面
        if (Inner.instance.isOrderPendingRepayment()) {//是否在待还款-未逾期
            Inner.instance.setLoanState(LoanState.Order_Pending_Repayment);
        } else if (Inner.instance.isOrderOverdueRepayment()) {//是否在待还款-已逾期
            Inner.instance.setLoanState(LoanState.Order_Overdue_Repayment);
        } else if (Inner.instance.isOrderLoaning()) {//是否在待放款
            Inner.instance.setLoanState(LoanState.Order_Loaning);
        } else if (Inner.instance.isLoanRejcted()) {//申请是否被拒绝
            Inner.instance.setLoanState(LoanState.Loan_Rejected);
        } else if (Inner.instance.isLoanReviewing()) {//是否有审核中的贷款
            Inner.instance.setLoanState(LoanState.Loan_Reviewing);
        } else {
            Inner.instance.setLoanState(LoanState.No_Order);
        }
    }

    private String product;
    private ArrayList<String> allStepTitles;
    private ArrayList<String> allSteps;
    private ArrayList<String> steps;
    private LoanState loanState;
    private String idCard;
    private String idCardImg;
    private String birthDate;
    private String applyStatus;//申请订单状态
    private String orderStatus;//还款订单状态
    private String deniedBetweenDay;//被拒重新申请天数
    private String userType;
    private JSONObject srcJSON;

    private String recipientName;

    public String getProduct() {
        if (TextUtils.isEmpty(product)) return "";
        return product;
    }

    public int getStepsSize(){
        if (steps==null) return 0;
        return steps.size();
    }

    public String getIdCard() {
        if (TextUtils.isEmpty(idCard)) return "";
        return idCard;
    }

    public String getIdCardImg() {
        if (TextUtils.isEmpty(idCardImg)) return "";
        return idCardImg;
    }

    public String getStepName(int index){
        if (steps==null||index<0||index>=steps.size()) return "";
        return steps.get(index);
    }

    public String getH5Url(){
        if (srcJSON==null) return "";
        return srcJSON.optString("h5");
    }

    public ProcessInfo_Step_prolenders getStep(int index){
        try {
            if (steps==null||index<0||index>=steps.size()||srcJSON==null) return null;
            String stepName = steps.get(index);

            String stepStr = "";

            if (srcJSON.has(stepName+"_desc")) stepStr = srcJSON.optString(stepName+"_desc");
            else stepStr = srcJSON.optString(stepName);


            if (TextUtils.isEmpty(stepStr) || stepStr.equalsIgnoreCase("null") || !stepStr.startsWith("{"))
                stepStr = AppShowCustomize_prolenders.getInstance().getOther(stepName+"_desc");

            if (TextUtils.isEmpty(stepStr)) return null;

            return new Gson().fromJson(stepStr, ProcessInfo_Step_prolenders.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public ArrayList<String> getSteps() {
        return steps;
    }

    public int getPercent(){
        if (steps==null||allSteps==null||steps.isEmpty()||allSteps.isEmpty()) return 0;
        int finishStepSize = allSteps.size()-steps.size();
        if (finishStepSize<0) return 100;
        int result = (int) Math.round(((double)finishStepSize)/((double)allSteps.size())*100);
        if (result == 100) result--;
        return result;
    }

    public void setLoanState(LoanState loanState) {
        this.loanState = loanState;
    }

    public String getOrderStatus() {
        if (TextUtils.isEmpty(orderStatus)) return "";
        return orderStatus;
    }

    public String getApplyStatus() {
        if (TextUtils.isEmpty(applyStatus)) return "";
        return applyStatus;
    }

    public String getDeniedBetweenDay() {
        if (TextUtils.isEmpty(deniedBetweenDay)) return "30";
        return deniedBetweenDay;
    }

    public ArrayList<String> getAllSteps() {
        if (allSteps==null) return new ArrayList<>();
        return allSteps;
    }

    public String getRecipientName() {
        if (TextUtils.isEmpty(recipientName)) return "";
        return recipientName;
    }

    public boolean isNewUser(){
        if (TextUtils.isEmpty(recipientName)) return true;
        return !userType.equalsIgnoreCase("OLD");
    }

    public String getStepTitle(int index){
        if (allStepTitles==null || index<0 || index>=allStepTitles.size()) return "";
        return allStepTitles.get(index);
    }

    /**
     * 待放款
     */
    public boolean isOrderLoaning() {
        return (getOrderStatus().equalsIgnoreCase(OrderStatus_prolenders.CREATED) ||
                getOrderStatus().equalsIgnoreCase(OrderStatus_prolenders.LENDING));
    }

    /**
     * 待还款-未逾期
     */
    public boolean isOrderPendingRepayment() {
        return getOrderStatus().equalsIgnoreCase(OrderStatus_prolenders.LOANED);
    }

    /**
     * 待还款-已逾期
     */
    public boolean isOrderOverdueRepayment() {
        return (getOrderStatus().equalsIgnoreCase(OrderStatus_prolenders.OVERDUE) ||
                getOrderStatus().equalsIgnoreCase(OrderStatus_prolenders.BAD_DEBT));
    }

    /**
     * 申请拒绝
     */
    public boolean isLoanRejcted() {
        return (getOrderStatus().equalsIgnoreCase(OrderStatus_prolenders.DENIED) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.SYS_DENIED) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.MANU_DENIED) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.DS_DENIED));
    }

    /**
     * 申请审核中
     */
    public boolean isLoanReviewing() {
        return (getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.CREATED) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.SYS_CHECK) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.SYS_APROVAL) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.MANU_APROVAL) ||
                getApplyStatus().equalsIgnoreCase(ApplyStatus_prolenders.DS_APROVAL));
    }

    public String getUserState(){
        if (isOrderPendingRepayment() || isOrderOverdueRepayment()) //订单状态-待还款
            return State_Rp;
        else if (isOrderLoaning() || isLoanReviewing()) //订单状态-放款中/审核中
            return State_Wait;
        else if (isLoanRejcted()) //订单状态-被拒绝
            return State_Fail;
        else return State_NoOrder;
    }

    public enum LoanState {
        ///////////////////
        No_Order,//没有订单
        Loan_Rejected,//申请拒绝
        Loan_Reviewing,//申请审核中
        Order_Loaning,//待放款
        Order_Pending_Repayment,//待还款，未逾期
        Order_Overdue_Repayment,//待还款,已逾期
    }
}
